package up_down_share.viewcontroller.coverviewpager;

import up_down_share.updaonwshare.v01.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.*;

public class MainActivity extends FragmentActivity implements PageScrolledListener {

	// --------------------------------------------------
	// ----- constants -----
	// --------------------------------------------------

	// --------------------------------------------------
	// ----- properties -----
	// --------------------------------------------------
     private int global_width = 0;
     private TextView text = null;
	// --------------------------------------------------
	// ----- extends -----
	// --------------------------------------------------

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_main);
	     
	
		final LayoutInflater inflater = LayoutInflater.from(this);
		final View v = inflater.inflate(R.layout.viewpager, null);
		text = (TextView)v.findViewById(R.id.textView1_viewpager);

		setContentView(v);
		
		

		final CustomViewPager page1 = (CustomViewPager) findViewById(R.id.pager1);
		final PagerAdapter adapter1 = new PagerAdapter(this);

		page1.setAdapter(adapter1);
		// adapter1.addPage(TestFragment.class, null);
		// adapter1.addPage(ListViewFragment.class, null);
		adapter1.addPage(fragment1.class, null);
		adapter1.addPage(fragment2.class, null);
		adapter1.addPage(fragment3.class, null);

	
		page1.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub

				if (event.getAction() == MotionEvent.ACTION_DOWN)
				//	System.out.println("Press down");
					;

				if (event.getAction() == MotionEvent.ACTION_UP)
					//System.out.println("Press release");
					;

				return false;
			}
		});
		
		page1.setPageScrolledListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onPageScrolled(int currentItem, int offsetPixzels) {
		// TODO Auto-generated method stub
		//int currentwidth = currentItem * this.getScreenWidth() + offsetPixzels;
		//System.out.println("MainActivity.onPageScrolled.currentwidth:"+currentwidth);
		//if(scrollview !=null)
			//scrollview.scrollTo(currentwidth, 0);
		
		System.out.println("MainActivity.onPageScrolled.currentItem"+currentItem);
		switch(currentItem){
			
			case 0:performTextChange("Item0");
			       break;
			case 1:performTextChange("Item1");
			       break;
			case 2:performTextChange("Item2");
			       break;
		    default: performTextChange("Item0");
		             break;
				
		}
			

	}

	// --------------------------------------------------
	// ----- functions -----
	// --------------------------------------------------
	private void displayWidth() {

		System.out.println("Screen width in pixels: " + getScreenWidth());
	}

	private int getScreenWidth() {
		// 获取屏幕的宽�?		
		final DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);

		return metric.widthPixels;

	}
	
	private void performTextChange( String content){
		
		text.setText(content);
	}

}
